package net.jarang.android.saleoffline.activity;

import net.jarang.android.saleoffline.SaleContext;
import net.jarang.android.saleoffline.domain.appinfo.AppManager;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

public class HomeActivity extends AbstractBaseActivity {
	TextView adverTitleTV;
	TextView footerDescTV;
	TextView tempTV;
	
	AppManager appManager;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		
		appManager = new AppManager(this);
		adverTitleTV = (TextView) findViewById(R.id.adverTitleTV);
		footerDescTV = (TextView) findViewById(R.id.footerDescTV);
		footerDescTV.setText("since 2011.08.01 by jarang.net");
		try {
		Gallery gallery = (Gallery) findViewById(R.id.mainAdver);
		gallery.setAdapter(new AdverImageAdapter(this));
		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				adverTitleTV.setText(position + " 번째 공통 광고입니다.");
			}
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
		} catch (Exception e) {
			Log.e(SaleContext.TAG, e.getMessage(), e);
		}
		tempTV = (TextView) findViewById(R.id.tempTV);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		tempTV.setText("회원 여부 : " + appManager.isMember() + "\n로그인여부 : " + appManager.isLogin());
	}
	
	public void logOutClick(View v) {
		appManager.updateLoginYn(false);
		onResume();
	}
	
	public void memberOutClick(View v) {
		appManager.updateMemberYn(false);
		onResume();
	}
	
	public class AdverImageAdapter extends BaseAdapter {
		private Context mContext;
		private int[] mImageIds = {
			R.drawable.adver1,
			R.drawable.adver2,
			R.drawable.adver3
		};

		public AdverImageAdapter(Context context) {
			mContext = context;
		}

		@Override
		public int getCount() {
			return mImageIds.length;
		}

		@Override
		public Object getItem(int position) {
			return mImageIds[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView;
			
			if (convertView == null) {
				imageView = new ImageView(mContext);
			} else {
				imageView = (ImageView)convertView;
			}
			
			imageView.setImageResource(mImageIds[position]);
			imageView.setScaleType(ImageView.ScaleType.MATRIX);
//			imageView.setLayoutParams(new Gallery.LayoutParams(136, 88));
			return imageView;
		}
	}	
}
